<?php
namespace app\common\model;

use app\admin\model\State;
use app\common\traits\Gate;
use think\Db;
use think\Model;
use think\facade\Cache;
use app\admin\model\Users;
use app\admin\model\Dq;
use app\admin\model\Group;
use app\admin\model\Depar;

class Leibie extends Model
{
    use Gate;
    protected $type = [
        'createtime'=>'timestamp:Y-m-d G:i',
        'edittime'=>'timestamp:Y-m-d G:i'
    ];
    /**
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getLeibieTextAttr($value, $data)
    {
        $status = [1=>'渠道',2=>'分类',3=>'收集医院',4=>'会所课程',5=>'会所状态',6=>'转介绍来源',7=>'医院渠道',8=>'邀约原因',9=>'医院课程'];
        return $status[$data['leibie']];
    }

    public function getYyxxTextAttr($value, $data)
    {
        $status = Leibie::yyxxkey();
        return $status[$data['name']];
    }
    //门诊
    public function getMenzhenTextAttr($value, $data){
        $status = Leibie::menzhenkey();
        return $status[$data['name']];
    }

    /**
     * @return array
     */
    public function getleibei(){
        $data = [['id'=>1,'name'=>'渠道'],['id'=>2,'name'=>'分类'],['id'=>3,'name'=>'收集医院'],['id'=>4,'name'=>'会所课程'],['id'=>5,'name'=>'会所状态'],['id'=>6,'name'=>'转介绍来源'],['id'=>7,'name'=>'医院渠道'],['id'=>8,'name'=>'邀约原因'],['id'=>9,'name'=>'医院课程']];
        return $data;
    }
    //订单类型
    public function getDdlx(){
        $data = [['id'=>1,'name'=>'系统建单'],['id'=>2,'name'=>'批量导入'],['id'=>3,'name'=>'转介绍建单']];
        return $data;
    }
    //邀约原因
    public function getYaoyueReason(){
        $reason  = Leibie::all(['leibie'=>8,'status'=>1]);
        return $reason;
    }

    public function getQudao(){
        if($this->hasRole(['qudaojingli','qudaozhuguan','qudaozhuanyuan','wangluojingli','wangluozhuguan','wangluozhuanyuan'])){
            $qudao = Leibie::all(['leibie'=>1,'status'=>1]);
        }else{
            $qudao = Leibie::all(['leibie'=>['in',[1,6]],'status'=>1]);
        }
        return $qudao;
    }
    public function getFenlei(){
        $fenlei  = Leibie::all(['leibie'=>2,'status'=>1]);
        return $fenlei;
    }
//    public function getYiyuan(){
//        $yiyuan = Leibie::all(['leibie'=>3,'status'=>1]);
//        return $yiyuan;
//    }
    public function getYiyuan($area=''){
        if(empty($area) || $area == 0){
            $yiyuan = Leibie::all(['leibie'=>3,'status'=>1]);
        }else{
            $yiyuan = Leibie::where(['status'=>1,'dq'=>$area,'leibie'=>3])->field(['id','name'])->select();
        }
        return $yiyuan;
    }
    public function getKecheng(){
        $kecheng = Leibie::all(['leibie'=>4,'status'=>1]);
        return $kecheng;
    }
    public function getHsstate(){
        $hsstate = Leibie::all(['leibie'=>5,'status'=>1]);
        return $hsstate;
    }
    public function getCity(){
        $area = Db::name('dq')->where(['status'=>1,'fid'=>0])->cache(600)->select();
        return $area;
    }
    public function getArea(){
        $area = Db::name('dq')->where(['status'=>1,'fid'=>['neq',0]])->cache(600)->select();
        return $area;
    }
    public function getQdkf(){
        $qdkf = Db::name('users')->where(['status'=>1,'depar_id'=>1])->cache(600)->select();
        return $qdkf;
    }
    public function getWlkf(){
        $wlkf = Db::name('users')->where(['status'=>1,'depar_id'=>2])->cache(600)->select();
        return $wlkf;
    }
    public function getYwstate(){
        $ywstate = Db::name('state')->cache(600)->select();
        return $ywstate;
    }
    public function getJdkf(){
        if($this->hasRole(['qudaojingli','wangluojingli','huodongjingli','yaoyuejingli'])){//渠道经理、网络经理
            $jdkf = Db::name('users')->where(['status'=>1,'depar_id'=>session('admin_user.depar_id')])->cache(600)->select();
        }elseif ($this->hasRole(['qudaozhuguan','wangluozhuguan'])){//渠道主管、网络主管(分组)
            $jdkf = Db::name('users')->where(['status'=>1,'depar_id'=>session('admin_user.depar_id')/*,'group_id'=>session('admin_user.group_id')*/])->cache(600)->select();
        }elseif ($this->hasRole(['yaoyuezhuguan'])){//邀约主管(分组)
            $jdkf = Db::name('users')->where(['status'=>1,'depar_id'=>session('admin_user.depar_id'),'ygroup_id'=>session('admin_user.ygroup_id')])->cache(600)->select();
        }elseif ($this->hasRole('huodongzhuguan')){
            $jdkf = Db::name('users')->where(['status'=>1,'depar_id'=>session('admin_user.depar_id'),'group_id'=>session('admin_user.group_id')])->cache(600)->select();
        }elseif ($this->hasRole(['qudaozhuanyuan','wangluozhunayuan','huodongzhuanyuan','yaoyuezhuanyuan'])){//专员
            $jdkf = Db::name('users')->where(['status'=>1,'id'=>session('admin_user.adminid')])->cache(600)->select();
        }else{
            $jdkf = Db::name('users')->where(['status'=>1,'depar_id'=>['in',[1,2,3,4]]])->cache(600)->select();
        }
        return $jdkf;
    }
    public function getYykf(){
        if($this->hasRole('yaoyuezhuguan')){
            $yykf = Db::name('users')->where(['status'=>1,'depar_id'=>3,'ygroup_id'=>session('admin_user.ygroup_id')])->cache(600)->select();
        }elseif ($this->hasRole('yaoyuezhuanyuan')){
            $yykf = Db::name('users')->where(['status'=>1,'depar_id'=>3,'id'=>session('admin_user.adminid')])->cache(600)->select();
        }else{
            $yykf = Db::name('users')->where(['status'=>1,'depar_id'=>3])->cache(600)->select();
        }
        return $yykf;
    }
    public function getHszy( $hsdzid='' ){/* 会所专员 */
        if(!empty($hsdzid)){//具体某个店的专员
            $hszy = Db::name('users')->where(['status'=>1,'depar_id'=>4,'group_id'=>$hsdzid])->cache(600)->select();
        }else{
            if($this->hasRole('huodongzhuguan')){
                $hszy = Db::name('users')->where(['status'=>1,'depar_id'=>4,'group_id'=>session('admin_user.group_id')])->cache(600)->select();
            }elseif ($this->hasRole('huodongzhuanyuan')){
                $hszy = Db::name('users')->where(['status'=>1,'depar_id'=>4,'id'=>session('admin_user.adminid')])->cache(600)->select();
            }else{
                $hszy = Db::name('users')->where(['status'=>1,'depar_id'=>4])->cache(600)->select();
            }
        }
        return $hszy;
    }
    public function getYycg(){/* 摄影场馆 */
        if($this->hasRole('sheyingdiaodu')){
            $yycg = Db::name('group')->where(['status'=>1,'depar_id'=>5,'id'=>session('admin_user.group_id')])->cache(600)->select();
        }else{
            $yycg = Db::name('group')->where(['status'=>1,'depar_id'=>5])->cache(600)->select();
        }
        return $yycg;
    }
    public function getHdmd(){/* 活动门店 */
        if($this->hasRole(['huodongzhuguan','huodongzhuanyuan'])){
            $hdmd = Db::name('group')->where(['status'=>1,'depar_id'=>4,'id'=>session('admin_user.group_id')])->cache(600)->select();
        }else{
            $hdmd = Db::name('group')->where(['status'=>1,'depar_id'=>4])->cache(600)->select();
        }
        return $hdmd;
    }

    public function getBumen(){
        $bumen = Db::name('depar')->where(['status'=>1])->cache(600)->select();
        return $bumen;
    }

    public function getMenzhen(){/*门诊*/
        $menzhen = Leibie::all(['leibie'=>7,'status'=>1]);
        return $menzhen;
    }

    public function getFkry(){//反馈人员
        if($this->hasRole(['qudaojingli','wangluojingli','huodongjingli','qudaozhuguan','wangluozhuguan'])){
            $fkry = Db::name('users')->where(['status'=>1,'depar_id'=>session('admin_user.depar_id')])->select();
        }elseif ($this->hasRole(['huodongzhuguan'])){
            $fkry = Db::name('users')->where(['status'=>1,'depar_id'=>session('admin_user.depar_id'),'group_id'=>session('admin_user.group_id')])->select();
        }elseif ($this->hasRole(['qudaozhuanyuan','wangluozhuanyuan','huodongzhuanyuan'])){
            $fkry = Db::name('users')->where(['status'=>1,'id'=>session('admin_user.adminid')])->select();
        }else{
            $fkry = Db::name('users')->where(['status'=>1,'depar_id'=>['in',[1,2,4]]])->select();
        }
        return $fkry;
    }

    public function getZjslaiyuan(){
        $zjslaiyuan = Leibie::all(['leibie'=>6,'status'=>1]);
        return $zjslaiyuan;
    }


    /**  条件筛选
     * @param $data   表单提交的数据
     * @return mixed  where条件
     */
    public  function map($data){
        //ID
        if(!empty($data['id'])){
            trim($data['id']);
            $map['id']=['eq',$data['id']];
        }
        //顾客姓名
        if(!empty($data['name'])){
            trim($data['name']);
            $map['name']=['eq',$data['name']];
        }
        //类别
        if(!empty($data['leibie'])){
            trim($data['leibie']);
            $map['leibie']=['eq',$data['leibie']];
        }
        $map['status'] = 1;//开启的订单
        return $map;
    }
    /*  新增或修改订单  */
    public function edit($data){
        //新建
        if(empty($data['id'])){
            $res = $this->save($data);
            //成功，更新日志
            if($res){
                return ['status'=>'1','msg'=>"新增成功",'url'=>url('index')];
            }else{//失败
                return ['status'=>'0','msg'=>$this->getError()];
            }
        }else{//更新

            $res = $this->isUpdate()->save($data);
            //成功
            if($res){
                return ['status'=>'1','msg'=>'数据更新成功！','url'=>url('index')];
            }else{//失败
                return ['status'=>'0','msg'=>$this->getError()];
            }
        }
    }
    /**
     * 医院信息取KEY
     * @return array
     */
    public static function yyxxkey(){
        $data = Cache::tag('yyxx')->get('yyxxdata');
        if(!$data){
            $data=model('leibie')->where('leibie',3)->column('id','name');
            Cache::tag('yyxx')->set('yyxxdata',$data);
        }
        return $data;
    }
    public static function menzhenkey(){
        $data = Cache::tag('menzhen')->get('menzhendata');
        if(!$data){
            $data=model('leibie')->where('leibie',7)->column('id','name');
            Cache::tag('menzhen')->set('menzhendata',$data);
        }
        return $data;
    }

    /**
     * 医院信息
     * @return array
     */
    public static function yyxx(){
        $data = Cache::tag('yyxx')->get('yyxxdata');
        if(!$data){
            $data=model('leibie')->where('leibie',3)->column('name','id');
            Cache::tag('yyxx')->set('yyxxdata',$data);
        }
        return $data;
    }
    //门诊
    public static function menzhen(){
        $data = Cache::tag('menzhen')->get('menzhendata');
        if(!$data){
            $data=model('leibie')->where('leibie',7)->column('name','id');
            Cache::tag('menzhen')->set('menzhendata',$data);
        }
        return $data;
    }
    /**
     * 课程信息
     * @return array
     */
    public static function kcxx(){
        $data = Cache::tag('kcxx')->get('kcxxdata');
        if(!$data){
            $data=model('leibie')->where('leibie',4)->column('name','id');
            Cache::tag('kcxx')->set('kcxxdata',$data);
        }
        return $data;
    }
    /**
     * 渠道信息
     * @return array
     */
    public static function qdxx(){
        $data = Cache::tag('qdxx')->get('qdxxdata');
        if(!$data){
            $data=model('leibie')->where('leibie','in',[1,6])->column('name','id');
            Cache::tag('qdxx')->set('qdxxdata',$data);
        }
        return $data;
    }

    //分类
    public static function lbxx(){
        $data = Cache::tag('lbxx')->get('lbxxdata');
        if(!$data){
            $data=model('leibie')->where('leibie',2)->column('name','id');
            Cache::tag('lbxx')->set('lbxxdata',$data);
        }
        return $data;
    }

    public static function hszt(){
        $data = Cache::tag('hszt')->get('hsztdata');
        if(!$data){
            $data=model('leibie')->where('leibie',5)->column('name','id');
            Cache::tag('hszt')->set('hsztdata',$data);
        }
        return $data;
    }

    /*地区*/
    public static function area(){
        $data = Cache::tag('area')->get('areadata');
        if(!$data){
            $dq = new Dq();
            $data=$dq->column('name','id');
            Cache::tag('area')->set('areadata',$data);
        }
        return $data;
    }
    /**
     * 门店信息
     * @return array
     */
    public static function mendian(){
        $data = Cache::tag('hsmd')->get('mendiandata');
        if(!$data){
            $group  = new Group();
            $data = $group->column('title','id');
            Cache::tag('hsmd')->set('mendiandata',$data);
        }
        return $data;
    }

    /**
     * 客服
     * @return array
     */
    public static function kefu(){
        $data = Cache::tag('users')->get('kefudata');
        if(!$data){
            $data=Users::column('username','id');
            Cache::tag('Users')->set('kefudata',$data);
        }
        return $data;
    }

    public static function fankui(){
       $data=Cache::tag('fankui')->get('fankuidata');
        if(!$data){
            $data=Users::column('username','id');
            Cache::tag('fankui')->set('fankuidata',$data);
        }
        return $data;
    }

    public static function bumen(){
        $data = Cache::tag('bumen')->get('bumendata');
        if(!$data){
            $depar = new Depar();
            $data = $depar->column('name','id');
            Cache::tag('bumen')->set('bumendata',$data);
        }
        return $data;
    }

    public static function zjsuser(){
        $data = Cache::tag('zjsuser')->get('zjsuser');
        if(!$data){
            $customer = new Customer();
            $data = $customer->column('username','id');
            Cache::tag('zjsuser')->set('zjsuser',$data);
        }
        return $data;
    }
    public static function zjsbumen(){
        $data = Cache::tag('zjsbumen')->get('zjsbumen');
        if(!$data){
            $depar = new Department();
            $data = $depar->column('sectionname','id');
            Cache::tag('zjsbumen')->set('zjsbumen',$data);
        }
        return $data;
    }

    public static function yyreason(){
        $data = Cache::tag('yyreason')->get('yyreasondata');
        if(!$data){
            $data=model('leibie')->where('leibie',8)->column('name','id');
            Cache::tag('yyreason')->set('yyreasondata',$data);
        }
        return $data;
    }



}

?>